//
//  ShellNodeView.m
//  ShellWallert
//
//  Created by lvzhao on 2019/10/24.
//  Copyright © 2019 VV. All rights reserved.
//

#import "ShellNodeView.h"
#import "ShellNodeCell.h"
#import "ShellNodeHeadView.h"
#import "LZBaseTableView.h"
#import "ShellNodeViewModel.h"



@interface  ShellNodeView()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) ShellNodeViewModel *viewModel;
@property (nonatomic,strong) LZBaseTableView *tableView;
@property (nonatomic,strong) ShellNodeHeadView *headView;


@end

@implementation ShellNodeView

- (instancetype)initViewModel:(LZBaseViewModel *)viewModel
{
    self = [super init];
    if (self) {
        self.viewModel = (ShellNodeViewModel*) viewModel;
        [self setupView];
    }
    return self;
}


#pragma mark - setupView
- (void)setupView{
    
    
    NSString *levelTextDesc = [NSString stringWithFormat:@"%@",self.viewModel.levelDictionary[@"levelTextDesc"]];
    CGFloat tipHeight = [levelTextDesc lz_textHeightWithFontSize:k_textFont(13) withMaxWidth:K_SCREENWIDTH - 50];

    self.headView = [[ShellNodeHeadView alloc] initViewModel:self.viewModel];
    self.headView.frame = CGRectMake(0, 0, K_SCREENWIDTH, tipHeight + 222);
    
    [self addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.right.bottom.mas_equalTo(0);
    }];
    self.tableView.tableHeaderView = self.headView;
    
    [self.tableView reloadData];
    
}



#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.viewModel.dataArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ShellNodeCell *profitCell = [tableView dequeueReusableCellWithIdentifier:@"ShellNodeCell"];
    if(self.viewModel.dataArray.count > indexPath.row){
        profitCell.indexPath = indexPath;
        profitCell.modelObject = self.viewModel.dataArray[indexPath.row];
    }
    return profitCell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView *sectionView = [[UIView alloc] init];
    sectionView.backgroundColor = COLOR_BACKGROUND;
    sectionView.frame = CGRectMake(0, 0, K_SCREENWIDTH, 40);
    //等级
    UILabel *timeLab = [UILabel lz_labelWithText:LZLocalizedString(@"等级") fontSize:k_textFont(12) color:UIColorHex(0x678DA8)];
    timeLab.adjustsFontSizeToFitWidth = YES;
    timeLab.frame = CGRectMake(22, 0, 30, 40);
    [sectionView addSubview:timeLab];
    
    //直属节点
    UILabel *ownBrokerLab = [UILabel lz_labelWithText:LZLocalizedString(@"直属节点") fontSize:k_textFont(12) color:UIColorHex(0x678DA8)];
    ownBrokerLab.frame = CGRectMake(70, 0, 50, 40);
    [sectionView addSubview:ownBrokerLab];
    
    //所有节点算力
    UILabel *allHashRateLab = [UILabel lz_labelWithText:LZLocalizedString(@"所有节点算力") fontSize:k_textFont(12) color:UIColorHex(0x678DA8)];
    allHashRateLab.frame = CGRectMake(130, 0, 80, 40);
    [sectionView addSubview:allHashRateLab];
    
    
    //矿池奖励
    UILabel *rewardLab = [UILabel lz_labelWithText:LZLocalizedString(@"矿池奖励") fontSize:k_textFont(12) color:UIColorHex(0x678DA8)];
    rewardLab.frame = CGRectMake(220, 0, K_SCREENWIDTH - 220 - 22, 40);
    [rewardLab lz_rightAlignment];
    [sectionView addSubview:rewardLab];
    
   
    return sectionView;
}

- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40;
}




#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark - get && set
- (LZBaseTableView *)tableView{
    if(!_tableView){
        _tableView = [[LZBaseTableView alloc] init];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = COLOR_BACKGROUND;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerClass:[ShellNodeCell class] forCellReuseIdentifier:@"ShellNodeCell"];
        _tableView.rowHeight = 50;
    }
    return _tableView;
}

@end
